package com.tsing.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.pojo.dto.TlUserDTO;
import com.tsing.pojo.entity.TlUser;
import com.tsing.service.AuthService;
import com.tsing.service.impl.AuthServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author 吴阳基
 * @datetime 2023/12/4 13:20
 */
public class AuthController {
    private final AuthService authService = new AuthServiceImpl();

    public PageResult<TlUser> list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer page = (Integer) jsonObject.get("page");
         page = (page != null && page > 0) ? page : 1;

        Integer size = (Integer) jsonObject.get("size");
         size = (size != null && size > 0) ? size : 10;

        Integer status = (Integer) jsonObject.get("status");
         status = (status != null) ? status : null;

        PageResult<TlUser> pageResult = authService.list(page, size, status);
        return pageResult;
    }
    public Result pass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());
        int id = jsonObject.getInteger("userId");
        int result = authService.pass(id);
        if (result > 0) {
            return Result.success();
        }
        return Result.error("启用失败！");
    }
    public  Result fail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());
        int id = jsonObject.getInteger("userId");

        int result = authService.fail(id);
        if (result > 0) {
            return Result.success();
        }
        return Result.error("封禁失败！");
    }

    public Result update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        TlUserDTO tlUserDTO = new TlUserDTO();

        tlUserDTO.setUserId(jsonObject.getLong("userId"));
        tlUserDTO.setUsername(jsonObject.getString("username"));
        tlUserDTO.setUserImage(jsonObject.getString("userImage"));
        tlUserDTO.setBookiPhone(jsonObject.getLong("bookiPhone"));
        tlUserDTO.setSignature(jsonObject.getString("signature"));
        tlUserDTO.setIntroduction(jsonObject.getString("introduction"));
        tlUserDTO.setSex(jsonObject.getLong("sex"));
        tlUserDTO.setStatus(jsonObject.getLong("status"));

        int result = authService.update(tlUserDTO);

        if (result > 0) {
            return Result.success();
        }
        return Result.error("更新失败！");
    }
}
